package com.sscaipiao.android.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.sscaipiao.android.R;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.User;
import com.sscaipiao.android.utils.BaseHelper;
import com.sscaipiao.android.utils.UserDataServiceHelper;

public class MoreActivity extends Activity implements OnClickListener{

	private ImageView account;
	private ImageView identify;
	private ImageView share;
	private ImageView security;
	private ImageView softInfo;
	private ImageView suggestion;
	private ImageView about;
	private Button back;
	
	private View commentView;
	private AlertDialog adc;
	
	private ProgressDialog mProgress = null;
	
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more);
		
		Intent i = this.getIntent();
		if (!i.equals(null)) {
			Bundle b = i.getExtras();
			if (b != null) {
				if (b.containsKey(Constants.USER)) {
					user = (User) b.get(Constants.USER);
				}
			}
		}

		account = (ImageView) findViewById(R.id.more_account);
		identify = (ImageView) findViewById(R.id.more_identify);
		share = (ImageView) findViewById(R.id.more_share);
		security = (ImageView) findViewById(R.id.more_security);
		softInfo = (ImageView) findViewById(R.id.more_soft_info);
		suggestion = (ImageView) findViewById(R.id.more_suggestion);
		about = (ImageView) findViewById(R.id.more_about);

		back = (Button) findViewById(R.id.more_button_back);
		
		account.setOnClickListener(this);
		identify.setOnClickListener(this);
		share.setOnClickListener(this);
		security.setOnClickListener(this);
		softInfo.setOnClickListener(this);
		suggestion.setOnClickListener(this);
		about.setOnClickListener(this);
		back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (R.id.more_account == v.getId()) {
			
			Intent intent = new Intent(MoreActivity.this, LoginActivity.class);
			Bundle b = new Bundle();
				user = new User();

			//user.setToken(retUser.getToken());
			b.putSerializable(Constants.USER, user);
			intent.putExtras(b);
			startActivity(intent);
		} else if (R.id.more_identify == v.getId()) {
			
			Intent intent = new Intent(MoreActivity.this, Regist2Activity.class);
			Bundle b = new Bundle();
			//user.setToken(retUser.getToken());
			b.putSerializable(Constants.USER, user);
			intent.putExtras(b);
			startActivity(intent);
		}else if (R.id.more_button_back == v.getId()) {
			
			//MoreActivity.this.setResult(RESULT_OK, intent);  
			MoreActivity.this.finish();  
		} else if (R.id.more_security == v.getId()) {
			
			Intent intent = new Intent(MoreActivity.this, SecurityActivity.class);
			Bundle b = new Bundle();
			//user.setToken(retUser.getToken());
			b.putSerializable(Constants.USER, user);
			intent.putExtras(b);
			startActivity(intent);
		}else if (R.id.more_suggestion == v.getId()) {
			
			//MoreActivity.this.setResult(RESULT_OK, intent);  
			createLoginAlert() ; 
		}
		
	}

	/*
	 * 创建用户登陆的对话框 登陆界面包含两个按钮 1按钮为登陆 2按钮为不登陆试玩
	 */
	private void createLoginAlert() {
		AlertDialog.Builder ad = new AlertDialog.Builder(this);
		ad.setTitle("意见反馈");
		commentView = LayoutInflater.from(this).inflate(R.layout.comment, null);
		ad.setView(commentView);

		ad.setPositiveButton("提交", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface arg0, int arg1) {

				EditText comment = (EditText) commentView
						.findViewById(R.id.comment_editText);


				String word = comment.getText().toString();
				
				// 生成登陆对话框
				mProgress = BaseHelper.showProgress(MoreActivity.this,
						null, "正在为你提交...", false, true);
				// ProgressDialog.show(Main.this, "请等待...", "正在为你登陆...",true);
				UserDataServiceHelper.addComment(MoreActivity.this, word, user);
				closeProgress();
			}
		});
		ad.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				adc.dismiss();
			}

		});

		adc = ad.create();
		adc.show();
	}
	
	void closeProgress() {
		try {
			if (mProgress != null) {
				mProgress.dismiss();
				mProgress = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
